package com.lgx.mall.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lgx.common.entity.IntraCity;
import com.lgx.common.web.Constants;
import com.lgx.common.web.Pagination;
import com.lgx.mall.core.service.IProductService;
import com.lgx.mall.core.vo.ProductVO;
import com.lgx.mall.view.service.IWebPageDetailService;


/**
 * 优品
 * 
 * @author acer
 *
 */
@Controller
@RequestMapping("/excellent")
public class ExcellentController {
	


	@Autowired
	private IWebPageDetailService webPageDetailService;
	
	public IWebPageDetailService getWebPageDetailService() {
		return webPageDetailService;
	}

	public void setWebPageDetailService(
			IWebPageDetailService webPageDetailService) {
		this.webPageDetailService = webPageDetailService;
	}

	@Autowired
	private IProductService productService;
	
	public IProductService getProductService() {
		return productService;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	@RequestMapping("/index.htm")
	public String index(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		String scoreType = request.getParameter("scoreType");
		if(StringUtils.isEmpty(scoreType))scoreType = "1";
		request.setAttribute("scoreType", scoreType);
		
		String pageNo = request.getParameter("pageNo");
		if(StringUtils.isEmpty(pageNo))pageNo = "1";
		request.setAttribute("pageNo", pageNo);
		
		return "excellent/index";
	}
	
	@RequestMapping("/head.ftl")
	public String head(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		return "excellent/head";
	}
	
	/**
	 * 共享优品
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/first.ftl")
	public String first(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		long categoryId = 113;
		List<ProductVO> details1 = this.webPageDetailService.findProductVOByAreaAndCid(null, categoryId);
		categoryId = 114;
		List<ProductVO> details2 = this.webPageDetailService.findProductVOByAreaAndCid(null, categoryId);
		categoryId = 115;
		List<ProductVO> details3 = this.webPageDetailService.findProductVOByAreaAndCid(null, categoryId);
		categoryId = 116;
		List<ProductVO> details4 = this.webPageDetailService.findProductVOByAreaAndCid(null, categoryId);
		
		
		request.setAttribute("details1", details1);	
		request.setAttribute("details2", details2);	
		request.setAttribute("details3", details3);	
		request.setAttribute("details4", details4);
		
		return "excellent/first";
	}
	/**
	 * 优品分类
	 * @param request
	 * @param response
	 * @return
	 */
	
	@RequestMapping("/excellent-pro-cate.ftl")
	public String excellentProCate(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		long categoryId = 136;
		List<ProductVO> details1 = this.webPageDetailService.findProductVOByAreaAndCid(null, categoryId);
		request.setAttribute("details1", details1);
		//初始化导航栏分类
		request.setAttribute("categorys", Constants.categorys);
		return "excellent/excellent-pro-cate";
	}
	/***
	 * 积分选择
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/excellent-product.ftl")
	public String excellentProduct(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		long categoryId = 2;
		String scoreType = request.getParameter("scoreType");	
		String pageNo = request.getParameter("pageNo");
		if(StringUtils.isEmpty(pageNo))pageNo = Constants.FIRST_PAGE_NUM;
		
		Pagination page = this.productService.findProductByCategoryAndPrice(categoryId, scoreType, 15, Integer.parseInt(pageNo));
		request.setAttribute("page", page);
		request.setAttribute("pageNo", pageNo);
		request.setAttribute("scoreType", scoreType);
		return "excellent/excellent-product";
	}
	
	/***
	 * 推荐商家
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/random-recom.ftl")
	public String randomRecom(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);

		long categoryId = 137;
		List<ProductVO> details1 = this.webPageDetailService.findProductVOByAreaAndCid(null, categoryId);
		categoryId = 138;
		List<ProductVO> details2 = this.webPageDetailService.findProductVOByAreaAndCid(null, categoryId);
		categoryId = 139;
		List<ProductVO> details3 = this.webPageDetailService.findProductVOByAreaAndCid(null, categoryId);
		categoryId = 140;
		List<ProductVO> details4 = this.webPageDetailService.findProductVOByAreaAndCid(null, categoryId);
		categoryId = 141;
		List<ProductVO> details5 = this.webPageDetailService.findProductVOByAreaAndCid(null, categoryId);
		categoryId = 142;
		List<ProductVO> details6 = this.webPageDetailService.findProductVOByAreaAndCid(null, categoryId);
		
		request.setAttribute("details1", details1);	
		request.setAttribute("details2", details2);	
		request.setAttribute("details3", details3);	
		request.setAttribute("details4", details4);
		request.setAttribute("details5", details5);	
		request.setAttribute("details6", details6);
		return "excellent/random-recom";
	}

}
